package com.company.cloud.acct.modules.app.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.acct.constant.AcctCashConfig;
import com.company.cloud.acct.modules.api.constant.AcctStateEnum;
import com.company.cloud.acct.modules.api.response.AccountModel;
import com.company.cloud.acct.modules.app.controller.req.AcctMergeParam;
import com.company.cloud.acct.modules.app.service.AcctOpService;
import com.company.cloud.acct.modules.base.entity.AccountInfo;
import com.company.cloud.acct.modules.base.service.AccountInfoService;
import com.company.cloud.acct.modules.cache.utils.AcctConvertUtils;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Login;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.user.modules.api.models.UserModel;
import com.company.cloud.user.modules.api.request.UserPtQuery4PageParam;
import com.company.cloud.user.modules.api.response.UserInfoQueryResp;
import com.company.cloud.user.modules.api.service.UserPlatformRemoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

@Log4j2
@Login
@RestController
@RequestMapping("/app/acct/info")
@Api(tags = "App_Acct_账户信息")
public class AcctInfoController {

    @Autowired
    private AccountInfoService accountInfoService;

    @Autowired
    private AcctOpService acctOpService;

    @Autowired
    private UserPlatformRemoteService userPlatformRemoteService;

    @PostMapping("/query")
    @ApiOperation(value = "用户列表查询")
    public BaseResult query(@RequestBody UserPtQuery4PageParam params) {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        params.setCompanyNo(userInfo.getCompanyNo());
        BaseClientResult<UserInfoQueryResp> baseClientResult = userPlatformRemoteService.queryUser(params);
        UserInfoQueryResp queryResp = baseClientResult.getData();
        for (UserModel entity : queryResp.getRecords()) {
            System.out.println("======entity===============" + entity.getId());
            final List<AccountInfo> list = accountInfoService.list(new QueryWrapper<AccountInfo>().eq("user_id", entity.getId()).eq("state", 1));
            entity.setPoint("0.00");
            entity.setBalance("0.00");
            entity.setFreezeFund("0.00");
            for (AccountInfo info : list) {
                if (!Objects.equals("point", info.getType())) {
                    entity.setBalance(info.getFund().toString());
                    entity.setFreezeFund(info.getFreezeFund().toString());
                } else {
                    entity.setPoint(info.getFund().toString());
                }
            }
        }
        return BaseResult.ok(queryResp);
    }

    @GetMapping("/getAcct")
    @ApiOperation(value = "我的账户信息", response = AccountModel.class)
    public BaseResult getAcct(@RequestParam("type") String type) {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        AccountInfo entity = accountInfoService.getOne(new QueryWrapper<AccountInfo>()
                .eq("user_id", userInfo.getId())
                .eq("type", type)
                .eq("state", AcctStateEnum.NORMAL.getCode())
        );
        if (entity == null) {
            BigDecimal balance = new BigDecimal("0.00");        // 税费
            AccountInfo accountInfo = new AccountInfo();
            accountInfo.setCompanyNo("TP");
            accountInfo.setUserId(userInfo.getId());
            accountInfo.setType("balance");
            accountInfo.setTotalFund(balance);
            accountInfo.setFund(balance);
            accountInfo.setFreezeFund(balance);
            accountInfo.setNotFund(balance);
            accountInfo.setState(AcctCashConfig.AccountState.APPLY.getCode());
            accountInfo.addCreateParam(userInfo);
            accountInfo.addEditParam(userInfo);
            accountInfoService.save(accountInfo);
        }
        AccountModel data = acctOpService.getAcctInfo(userInfo.getId(), type);
        return BaseResult.ok(data);
    }

    @PostMapping("/getAcctMerge")
    @ApiOperation(value = "我的账户信息列表", response = AccountModel.class)
    public BaseResult getAcctMerge(@RequestBody AcctMergeParam params) {
        ValidatorUtils.validate(params);
        if (CollectionUtils.isEmpty(params.getTypes())) {
            throw new BusinessException(BaseReturnEnum.PARAM_NULL);
        }
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        List<AccountInfo> list = accountInfoService.list(new QueryWrapper<AccountInfo>()
                .eq("user_id", userInfo.getId())
                .eq("state", AcctStateEnum.NORMAL.getCode())
                .in("type", params.getTypes())
        );
        AccountModel data = AcctConvertUtils.getAccountMerge(list);
        return BaseResult.ok(data);
    }

    @PostMapping("/getAcctList")
    @ApiOperation(value = "我的账户信息列表", response = AccountModel.class)
    public BaseResult getAcctList(@RequestBody AcctMergeParam params) {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        List<AccountInfo> list = accountInfoService.list(new QueryWrapper<AccountInfo>()
                .eq("user_id", userInfo.getId())
                .eq("state", AcctStateEnum.NORMAL.getCode())
                .in("type", params.getTypes())
        );
        if (CollectionUtils.isEmpty(list)) {
            return BaseResult.ok();
        }
        List<AccountModel> data = new ArrayList<AccountModel>();
        list.forEach(item -> {
            data.add(AcctConvertUtils.getAccountModel(item));
        });
        return BaseResult.ok(data);
    }

//    @GetMapping("/getAccountRemote")
//    @ApiOperation(value="测试client")
//    public BaseResult getAccountRemote() {
//        LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
////        AcctGetRequest request = new AcctGetRequest();
////        request.setCompanyNo(userInfo.getCompanyNo());
////        request.setUserId(userInfo.getId());
////        request.setType(AcctTypeEnum.Balance_Acct.getCode());
////
////        BaseClientResponse<AccountModel> data = accountRemoteService.getAcct(request);
//
//        AcctOpRequest request = new AcctOpRequest();
////        request.setCompanyNo(userInfo.getCompanyNo());
//        request.setUserId(userInfo.getId());
//        request.setType(AcctTypeEnum.Balance_Acct.getCode());
//        request.setIsAcctDetail(BaseConstant.YNEnum.YES.getCode());
//        request.setOpType(AcctOpTypeEnum.Add_Acct.getCode());
//
//        request.setRelObjType(IRelBusiType.PRD_Product);
//        request.setRelObjNo("PRD_TEST");
//        request.setFundOp(new BigDecimal(100));
//        request.setFundReal(new BigDecimal(100));
//        request.setMemo("测试MEMO");
//        request.setRelBizName("新增金额");
//        request.setRelDate("2020-09-15 11:11:11");
//
//        request.setInOut(AcctInOutEnum.In_Acct.getCode());
//        request.setBusiType("测试");
//
//        request.setOpUserId(userInfo.getId());
//        request.setOpIp(userInfo.getIp());
//
//
//        // 过期时间
//        // request.setOver overdue_date
////        BaseClientResponse<String> data = accountRemoteService.opAcct(request);
//
//        return BaseResult.ok();
//    }


}
